package com.jf.blog_jf04.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jf.blog_jf04.bean.Blog;
import com.jf.blog_jf04.bean.Category;
import com.jf.blog_jf04.data.PageInfo;
import com.jf.blog_jf04.data.dto.CategoryDto;
import com.jf.blog_jf04.data.ov.CategorySearchOV;
import com.jf.blog_jf04.mapper.CategoryMapper;
import com.jf.blog_jf04.service.BlogService;
import com.jf.blog_jf04.service.CategoryService;
import com.jf.blog_jf04.utils.BeanConvertUtils;
import com.jf.blog_jf04.utils.LoginUtils;
import com.jf.blog_jf04.utils.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * @author pang6
 * @description 针对表【category(博客分类表)】的数据库操作Service实现
 * @createDate 2025-10-18 14:32:11
 */
@Service
@Transactional
public class CategoryServiceImpl extends ServiceImpl<CategoryMapper, Category>
        implements CategoryService {
    @Autowired
    private BlogService blogService;

    @Override
    public R getPage(PageInfo pageInfo, CategorySearchOV ov) {
        QueryWrapper<Category> qw = new QueryWrapper<>();
        if (ov.getType() == 1) {
            qw.eq(ov.getCreateUser() != null, "create_user", ov.getCreateUser());
        } else {
            qw.eq("create_user", LoginUtils.getLoginUserinfo().getUserinfoId());
        }
        IPage<Category> iPage = this.page(pageInfo.setPage(), qw
                .eq(ov.getStatus() != null, "status", ov.getStatus())
                .like(StringUtils.hasText(ov.getName()), "name", ov.getName())
        );
        List<CategoryDto> list = BeanConvertUtils.coptList(iPage.getRecords(), CategoryDto.class);
        return R.success(pageInfo.setPageInfo(iPage.getTotal(), list));
    }

    @Override
    public R delete(List<Integer> ids) {
        /*获取所有关联的博客列表*/
        List<Blog> blogList = blogService.list(new QueryWrapper<Blog>().in("category_id", ids));
        /*去重获取有关联分类编号*/
        Set<Integer> blogCategoryIds = blogList.stream().map(Blog::getCategoryId).collect(Collectors.toSet());
        /*获取未被使用的分类编号*/
        List<Integer> list = ids.stream().filter(item -> !blogCategoryIds.contains(item)).collect(Collectors.toList());
        if (!list.isEmpty()) {
            this.removeBatchByIds(list);
        }
        String info = null;
        if (!blogCategoryIds.isEmpty()) {
            if (list.isEmpty()) {
                info = "编号" + blogCategoryIds + "已被使用，禁止删除！";
            } else {
                info = "编号" + list + "删除成功,编号" + blogCategoryIds + "已被使用，禁止删除！";
            }
        }
        return R.success(info);
    }

    @Override
    public R getSelect(int type) {
        List<Category> list = this.list(new QueryWrapper<Category>()
                .eq(type == 2, "create_user", LoginUtils.getLoginUserinfo().getUserinfoId())
        );
        return R.success(BeanConvertUtils.coptList(list, CategoryDto.class));
    }
}




